package zhxt.helmet.dao.provider;
import org.apache.ibatis.jdbc.SQL;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

public class ResultSqlProvider {
    public String buildSelectResultsByCameraIdsSql(Map<String, Object> params) {
        // 从参数中获取对应的参数值
        List<Integer> cameraIdList = (List<Integer>) params.get("cameraIdList");
        Timestamp startTime = (Timestamp) params.get("startTime");
        Timestamp endTime = (Timestamp) params.get("endTime");

        SQL sql = new SQL();
        sql.SELECT("*");
        sql.FROM("result");

        // 处理多个摄像头 ID 的情况
        if (cameraIdList!= null && cameraIdList.size() > 0) {
            StringBuilder inClause = new StringBuilder("camera_id IN (");
            for (int i = 0; i < cameraIdList.size(); i++) {
                inClause.append(cameraIdList.get(i));
                if (i < cameraIdList.size() - 1) {
                    inClause.append(",");
                }
            }
            inClause.append(")");
            sql.WHERE(inClause.toString());
        }

        // 添加时间范围条件
        if (startTime!= null && endTime!= null) {
            sql.WHERE("detect_time BETWEEN '" + startTime + "' AND '" + endTime + "'");
        }

        return sql.toString();
    }
}
